<?php 
/**
 * Class description
 *
 * @since 13 мая 2010
 * @author diotheos
 */
class Lib_Utility {
	/**
	 * Function description
	 *
	 * @since 13 мая 2010
	 * @author diotheos
	 */
	public static function filter($data = array(), $identity = 'id', $credential = 'name') {
            $filteredData = array();
            $identFunction = 'get'.ucfirst($identity);
            $contextFunction = 'get'.ucfirst($credential);
            
            foreach ($data as $row){
                if(is_array($row)){
                    $filteredData[] = array($identity => $row[$identity], $credential => $row[$credential]);
                } else {
                    if(method_exists($row, $identFunction) && method_exists($row, $contextFunction)){
                        $filteredData[] = array($identity => $row->$identFunction(), $credential => $row->$contextFunction());
                    } else {
                        $filteredData[] = array($identity => $row->$identity, $credential => $row->$credential);
                    }
                }
            }
            return $filteredData;
	}
	/**
	 * Function description
	 * 
	 * @since 15 мая 2010
	 * @author diotheos
	 * @return void
	 */
	public static function createDefaultMultiOption(Zend_Form &$form, $element, $data = null, $defaultText = 'Все'){
	    $defaultVal = new stdClass();
		$defaultVal->id = -1;
		$defaultVal->name = _($defaultText);
		$data = (null === $data)?array($defaultVal):$data;
		return self::createMultiOptions($form,$element,$data);
	}
	/**
	 * Function description
	 * 
	 * @since 13 мая 2010
	 * @author diotheos
	 * @return bool
	 */
	public static function createMultiOptions(Zend_Form &$form, $formElement, &$data, $identificateField = 'id', $contextualField = 'name'){
		if(!count($data)){
            return false;
        }
        foreach ($data as $row){
            if(is_array($row)){
                $formElement->addMultiOption($row[$identificateField], html_entity_decode($row[$contextualField], ENT_COMPAT, 'UTF-8'));
            } else {
                $identFunction = 'get'.ucfirst($identificateField);
                $contextFunction = 'get'.ucfirst($contextualField);

                if(method_exists($row, $identFunction) && method_exists($row, $contextFunction)){
                    $formElement->addMultiOption($row->$identFunction(), html_entity_decode($row->$contextFunction(), ENT_COMPAT, 'UTF-8'));
                } else {
                    $formElement->addMultiOption($row->$identificateField, html_entity_decode($row->$contextualField, ENT_COMPAT, 'UTF-8'));
                }
            }
        }
        return true;
	}
	
	/**
	 * Function description
	 * 
	 * @since 15 мая 2010
	 * @author diotheos
	 * @return void
	 */
	public static function createObjectSet(&$data = array()){
		$objectSet = array();
		$i = 0;
		foreach($data as $row){
			$objectSet[$i] = new stdClass();
			foreach($row as $key=>$value){
				$objectSet[$i]->$key = $value;
			}
			$i++;
		}
		return $objectSet;
	}
	
	/**
	 * Function description
	 * 
	 * @since 15 мая 2010
	 * @author diotheos
	 * @return void
	 */
	public static function getRangeObjectSet($from, $to){
		$objectSet = array();
		if($from < $to){
	        for($i = $from; $i<=$to; $i++){
	            $objectSet[$i] = new stdClass();
	            $objectSet[$i]->id = $objectSet[$i]->name = $i;
	        }
		} else {
		  for($i = $from; $i>=$to; $i--){
                $objectSet[$i] = new stdClass();
                $objectSet[$i]->id = $objectSet[$i]->name = $i;
            }
		}
        return $objectSet;
	}
	
}